#!/usr/bin/env python

import unittest

from rawdb import RawDB
from db import DB, VersionedDB, DBNoTransactionException, DBInvalidId


class DB_Test( unittest.TestCase ):
    def setUp( self ):
        self.db = DB( RawDB() )
    # setUp()


    def testCase1( self ):
        self.db.begin()
        self.db.set( 1, 1 )
        self.assertEqual( self.db.get( 1 ), 1 )
        self.db.end()

        self.db.begin()
        self.assertEqual( self.db.get( 1 ), 1 )
        self.db.end()
    # testCase1()


    def testCase2( self ):
        self.db.begin()
        self.db.set( 1, 1 )
        self.assertEqual( self.db.get( 1 ), 1 )
        self.db.end()

        self.db.begin()
        self.assertEqual( self.db.get( 1 ), 1 )
        self.db.end()

        self.db.begin()
        self.db.set( 1, 2 )
        self.assertEqual( self.db.get( 1 ), 2 )
        self.db.abort()

        self.db.begin()
        self.assertEqual( self.db.get( 1 ), 1 )
        self.db.end()
    # testCase2()

    def testCase3( self ):
        self.db.begin()
        self.assertRaises( DBInvalidId, self.db.get, 10000 )
        self.db.end()
    # testCase3()


### These were removed since db.autolock property was removed
##     def testCase4( self ):
##         self.db.autolock = False
##         self.assertRaises( DBNoTransactionException, self.db.set, 1, 1 )
##         self.assertRaises( DBNoTransactionException, self.db.get, 1  )
##         self.assertRaises( DBNoTransactionException, self.db.end )
##         self.assertRaises( DBNoTransactionException, self.db.abort )
##     # testCase4()


##     def testCase5( self ):
##         self.db.autolock = False
##         self.db.begin()

##         self.assertRaises( DBNoLockException, self.db.set, 1, 1 )
##         self.assertRaises( DBNoLockException, self.db.get, 1 )
##         self.db.abort()
##     # testCase5()


##     def testCase6( self ):
##         self.db.autolock = False
##         self.db.begin()

##         self.db.xlock( 1 )
##         self.db.set( 1, 1 )
##         self.db.end()

##         self.db.begin()
##         self.db.slock( 1 )
##         self.assertEqual( self.db.get( 1 ), 1 )
##         self.db.end()

##         self.db.begin()
##         self.db.xlock( 1 )
##         self.assertEqual( self.db.get( 1 ), 1 )
##         self.db.end()

##         self.db.begin()
##         self.db.slock( 1 )
##         self.assertRaises( DBNoLockException, self.db.set, 1, 2 )
##         self.db.end()
##     # testCase6()
# DB_Test


class VersionedDB_Test( unittest.TestCase ):
    def setUp( self ):
        self.db = VersionedDB( RawDB(), [] )
    # setUp()


    def testCase1( self ):
        self.db.begin()
        self.db.set( 1, 1 )
        self.assertEqual( self.db.get( 1 ), 1 )
        self.db.end()

        self.db.begin()
        self.assertEqual( self.db.get( 1 ), 1 )
        self.db.end()
    # testCase1()


    def testCase2( self ):
        self.db.begin()
        self.db.set( 1, 1 )
        self.assertEqual( self.db.get( 1 ), 1 )
        self.db.end()

        self.db.begin()
        self.assertEqual( self.db.get( 1 ), 1 )
        self.db.end()

        self.db.begin()
        self.db.set( 1, 2 )
        self.assertEqual( self.db.get( 1 ), 2 )
        self.db.abort()

        self.db.begin()
        self.assertEqual( self.db.get( 1 ), 1 )
        self.db.end()
    # testCase2()

    def testCase3( self ):
        self.db.begin()
        self.assertRaises( DBInvalidId, self.db.get, 10000 )
        self.db.end()
    # testCase3()
# VersionedDB_Test


if __name__ == "__main__":
    unittest.main()
# endif
